﻿<%@ WebHandler Language="C#" Class="AdminHandler" %>
using System;
using Common.Model;
using System.Web;
using Bussiness.Interface;
using Bussiness.Service;
using Newtonsoft.Json;
using Model;
using Common;
/// <summary>
/// AdminHandler 的摘要说明
/// </summary>
public class AdminHandler : IHttpHandler
{
    private static IBaseService _iBaseService = new BaseService();

    /// <summary>
    /// 表名
    /// </summary>
    public static string TableName;

    /// <summary>
    /// 方法名(数据的增删改查)
    /// </summary>
    public static string MethodName;

    /// <summary>
    /// 数据
    /// </summary>
    public static string Data;

    ///// <summary>
    ///// 返回客户端数据
    ///// </summary>
    public static string result;
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request["TableName"] != null && context.Request["MethodName"] != null && context.Request["Entity"] != null)
        {
            TableName = context.Request["TableName"];
            MethodName = context.Request["MethodName"];
            Data = context.Request["Entity"];
            try
            {
                switch (TableName)
                {
                    case "Orders":
                        result = AUD(JsonConvert.DeserializeObject<Orders>(Data), MethodName,TableName);
                        break;
                    case "Books":
                        result = AUD(JsonConvert.DeserializeObject<Books>(Data), MethodName,TableName);
                        break;
                    case "Users":
                        result = AUD(JsonConvert.DeserializeObject<Users>(Data), MethodName,TableName);
                        break;
                    default:
                        return;
                }
            }
            catch {
                result = JsonConvert.SerializeObject(false);
            }
            context.Response.Write(result);
        }
    }

    #region 表格数据的增删改方法
    //<summary>
    // 表格数据的增删改方法
    // </summary>
    // <typeparam name = "T" > 类型 </ typeparam >
    // < param name="entity">实体</param>
    // <param name = "MethodName" > 操作方法名 </ param >
    // < returns ></ returns >
    public static string AUD<T>(T entity, string MethodName,string TableName) where T : BaseModel
    {
        Result<bool> resultbool = new Result<bool>();
        switch (MethodName)
        {
            case "Add":
                switch (TableName)
                {
                    case "Orders":
                        Orders order = new Orders();
                        order = entity as Orders;
                        order.CopUser = StaticConstant.CopUser;
                        order.OrderID = Guid.NewGuid().ToString();
                        order.BookingTime = DateTime.Now;

                        resultbool = _iBaseService.Insert(order);
                        break;
                    case "Books":
                        Books book = new Books();
                        book = entity as Books;
                        book.BookID = Guid.NewGuid().ToString();
                        book.StockTime = DateTime.Now;

                        resultbool = _iBaseService.Insert(book);
                        break;
                    case "Users":
                        Users user = new Users();
                        user = entity as Users;
                        user.UserID = Guid.NewGuid().ToString();
                        user.Password = MD5Encrypt.Encrypt(user.Password);
                        user.Time = DateTime.Now;

                        resultbool = _iBaseService.Insert(user);
                        break;
                    default:
                        return JsonConvert.SerializeObject(false);
                }
                break;
            case "Update":
                if (TableName == "Orders")
                {
                    Orders order = new Orders();
                    order = entity as Orders;
                    order.CopUser = StaticConstant.CopUser;
                    resultbool = _iBaseService.Update(entity);
                }
                else {
                    resultbool = _iBaseService.Update(entity);
                }
                break;
            case "Delete":
                resultbool = _iBaseService.Delete(entity);
                break;
            default:
                return JsonConvert.SerializeObject(false);
        }
        return JsonConvert.SerializeObject(resultbool.Data);
    }
    #endregion

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}